// miniprogram/pages/home/home.js
var table=wx.cloud.database().collection("shibie");
var duixiang=require("../tool/come");
var app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
     isregister:false,
     openid:'',
     base64old:'',
     denlu:false
  },
  onLoad: function (options) {
    var that=this;
    this.setData({
      denlu:app.globalData.denlu
    })
    wx.cloud.callFunction({
      // 要调用的云函数名称
      name: 'getOpenid',
      // 传递给云函数的event参数
      data: {}
    }).then(res => {
      console.log(res);
      that.setData({
        openid:res.result.openid
      })
      table.where({
        _openid:res.result.openid
      }).get({
        success:function(res){
          console.log(res);
          if(res.data.length>0){
            that.setData({
              isregister:true,
              base64old:res.data[0].base64
            })
          }
        }
      })
    }).catch(err => {
      console.log(err)
    })
  },
  login:function(){
    var that=this;
    const ctx=wx.createCameraContext();
    duixiang.getPicFromcenavm(ctx).then(res=>{
      var base640=res;
    //现在的图片  console.log("base640:"+base640);
    //之前的图片  console.log("baseold:"+that.data.base64old);
    //图片比对
    wx.request({
      url: 'https://api.jisuapi.com/facecompare/compare?appkey='+app.globalData.appkey,
      method:"post",
      data:{
        pic:base640,
        pic2:that.data.base64old
      },
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
        console.log(res);
        if(res.data.result.score>80){
          wx.showToast({
            title: '恭喜你找到失踪老人',
          })
        }else{
          wx.showToast({
            title: '他(她)不是失踪老人',
          })
        }
      }
    })
      })
  }
})